<?php

namespace CHOMP\SGIBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CHOMP\SGIBundle\Entity\Estado;
use CHOMP\SGIBundle\Form\EstadoType;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineORMAdapter;

/**
 * Estado controller.
 *
 * @Route("/estado")
 */
class EstadoController extends Controller
{

    /**
     * Lists all Estado entities.
     *
     * @Route("/index/{page}", name="estado_index", requirements={"page" = "\d+"}, defaults={"page" = "1"})
     * @Method("GET")
     * @Template("CHOMPSGIBundle:Estado:index.html.twig")
     */
    public function indexAction($page)
    {
        $em = $this->getDoctrine()->getManager();
		$query = $em->createQueryBuilder()->select('u')->from('CHOMPSGIBundle:Estado','u')->where('u.borrado = false');
		
		$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
		$pagerfanta->setMaxPerPage(10);
		$pagerfanta->setCurrentPage($page);
		
		return array('entities' => $pagerfanta);
    }
    /**
     * Creates a new Estado entity.
     *
     * @Route("/", name="estado_create")
     * @Method("POST")
     * @Template("CHOMPSGIBundle:Estado:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new Estado();
		$entity->setBorrado(false);
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('estado_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to create a Estado entity.
    *
    * @param Estado $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(Estado $entity)
    {
        $form = $this->createForm(new EstadoType(), $entity, array(
            'action' => $this->generateUrl('estado_create'),
            'method' => 'POST',
        ));

        return $form;
    }

    /**
     * Displays a form to create a new Estado entity.
     *
     * @Route("/new", name="estado_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Estado();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a Estado entity.
     *
     * @Route("/{id}", name="estado_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('CHOMPSGIBundle:Estado')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Estado entity.');
        }

        return array(
            'entity'      => $entity,
        );
    }

    /**
     * Displays a form to edit an existing Estado entity.
     *
     * @Route("/{id}/edit", name="estado_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('CHOMPSGIBundle:Estado')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Estado entity.');
        }

        $form = $this->createEditForm($entity);

        return array(
            'entity'      => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to edit a Estado entity.
    *
    * @param Estado $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Estado $entity)
    {
        $form = $this->createForm(new EstadoType(), $entity, array(
            'action' => $this->generateUrl('estado_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        return $form;
    }
    /**
     * Edits an existing Estado entity.
     *
     * @Route("/{id}", name="estado_update")
     * @Method("PUT")
     * @Template("CHOMPSGIBundle:Estado:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('CHOMPSGIBundle:Estado')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Estado entity.');
        }

        $form = $this->createEditForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('estado_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'form'   => $form->createView(),
        );
    }
    /**
     * Deletes a Estado entity.
     *
     * @Route("/delete/{id}", name="estado_delete")
     * @Method("GET")
     */
	public function deleteAction($id) {
		
		$em = $this->getDoctrine()->getManager();
		$idsArray = explode (',', $id);

		for ($i = 0; $i < count($idsArray); $i++){
			$empABorrar = $this->getDoctrine()->getRepository('CHOMPSGIBundle:Estado')->find($idsArray[$i]);
			$empABorrar->setBorrado(TRUE);
			$em->persist($empABorrar);
		}

		$em->flush();
				
		return $this->redirect($this->generateUrl('estado_index'));	
	}
	
}
